Dynamic Account Group Documentation

Step-by-step developer guide on how selecting Account Head dynamically loads Account Groups in the Account Ledger Create Form.

Module Architecture Overview

Component Type File / Location Description
Form View resources/views/backend/account-ledger/create.blade.php Contains the main HTML select fields for Account Type and Account Group.
Partial View resources/views/backend/account-ledger/type_base_group.blade.php Generates the dynamic <option> HTML based on the returned controller data.
Controller AccountLedgerController.php Handles the AJAX request, fetches data using the model, and returns a compiled HTML view.
Javascript public/backend/js/main.js Listens for the change event and executing the AJAX request.
Model App\Models\AccountGroup Eloquent model interacting with the database to fetch related groups based on head/type ID.

Implementation Steps

1

HTML Select Elements Setup

The Account Type (Head) dropdown has the class ._account_head_id. When a user changes the value of this field, it acts as the trigger. The target element that will be updated has the class ._account_groups.

Code Snippet (create.blade.php):

<!-- Trigger Source: Account Head Select -->
<select class="form-control _account_head_id" name="_account_head_id" type_base_group="{{ url('type_base_group') }}" required>
    <option value="">--Select Account Type--</option>
    @forelse($account_types as $account_type)
        <option value="{{ $account_type->id }}">{{ $account_type->_name }}</option>
    @empty
    @endforelse
</select>

<!-- Target Output: Account Group Select -->
<select class="form-control _account_groups" name="_account_group_id" required>
    <option value="">--Select Account Group--</option>
</select>
2

jQuery Event Listener & AJAX Call

In main.js, an event listener is attached to the ._account_head_id dropdown. When an option is selected, the script retrieves the selected ID and fires an AJAX GET request to the base URL appended with type_base_group.

$(document).on("change", "._account_head_id", function() {
    var _account_head_id = $(this).val();
    var action_url = project_base_url + "/type_base_group";
    
    var request = $.ajax({
        url: action_url,
        method: "GET",
        data: { id : _account_head_id },
        dataType: "html"
    });
    
    request.done(function( msg ) {
        // Injects the dynamically compiled HTML into the Account Group dropdown
        $(document).find("._account_groups").html( msg );
    });
});
3

Controller Logic & Database Retrieval

The GET request hits the type_base_group route which calls the type_base_group() method in AccountLedgerController.php. This queries the account_groups database table using the AccountGroup Eloquent model.

It fetches all groups that match the passed _account_head_id (which comes through as $request->id).

public function type_base_group(Request $request) {
    // Filter groups based on the account head ID securely
    $account_groups = AccountGroup::where('_account_head_id', $request->id)
                                    ->orderBy('_name', 'asc')
                                    ->get();
                                    
    // Passes the data to a dedicated partial view
    return view('backend.account-ledger.type_base_group', compact('account_groups'));
}
4

HTML Rendering & Injection

The data is injected into the type_base_group.blade.php file which iterates over the retrieved collection and creates a stream of <option> HTML elements. Finally, this raw HTML template is returned as a string to the AJAX success callback, replacing the existing Account Group dropdown elements.

<option value="">--Select Account Group--</option>
@forelse($account_groups as $account_group)
    <option value="{{ $account_group->id }}">
        {{ $account_group->id }} - {{ $account_group->_name }}
    </option>
@empty
    <!-- Handles cases with no groups -->
@endforelse

Summary Flow Diagram

[USER Action] Selects Account Type (ID=1) │ ▼ [jQuery] Listens to 'change' on ._account_head_id ───► Extacts value ID=1 │ ▼ [AJAX GET] Request to /type_base_group?id=1 │ ▼ [Controller] AccountLedgerController@type_base_group │ ▼ [Model] AccountGroup::where('_account_head_id', 1) ───► Fetches from DB │ ▼ [Blade Partial] type_base_group.blade.php ────────────┬► HTML <option> tags generated │ [jQuery.done] ◄───────────────────────────────────────┘ │ ▼ [UI DOM] Replaces ._account_groups inner HTML with new options